﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace DragonAgeConsoleTrainer.Controls
{
	public partial class CodeGrid : UserControl
	{
		//	We're sending up/down/pgup/pgdown/home/end keystrokes from the filter textbox to the grid.
		//	We don't actually have to send input like we do with Dragon Age, just invoke the protected
		//	OnKeyDown with the proper KeyEvent. Being somewhat lazy using reflection rather than 
		//	inheriting from DataGridView, inherited framework controls like to die in the Windows
		//	Forms designer anyhow.
		private static MethodInfo dataGridOnKeyDown;
		static CodeGrid()
		{
			dataGridOnKeyDown = typeof(DataGridView).GetMethod("OnKeyDown", BindingFlags.Instance | BindingFlags.NonPublic);
		}

		private string filterTemplate = string.Empty;

		public DataTable Codes
		{
			get 
			{
				return dataGridView.DataSource as DataTable; 
			}
			set 
			{ 
				dataGridView.DataSource = value;

				if (value != null)
				{
					dataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

					//	Filter applies to all text columns in the grid
					filterTemplate = "1=0";
					foreach (DataColumn col in value.Columns)
					{
						if (col.DataType == typeof(string))
							filterTemplate += " OR " + col.ColumnName + " LIKE '%{0}%'";
					}
				}
			}
		}        

		public DataGridView Grid
		{
			get
			{
				return dataGridView;
			}
		}

		public DataRow Code
		{
			get 
			{
				if (dataGridView.SelectedRows.Count == 0)
					return null;

				return (dataGridView.SelectedRows[0].DataBoundItem as DataRowView).Row; 
			}
		}
        
		public CodeGrid()
		{
			InitializeComponent();

			if (!DesignMode)
				dataGridView.DefaultCellStyle.Font = txtFilter.Font = Program.FixedWidthFont;
		}

		private void txtFilter_TextChanged(object sender, EventArgs e)
		{
			Codes.DefaultView.RowFilter = string.Format(filterTemplate, txtFilter.Text.Replace("\'", "\'\'"));
		}

		private void txtFilter_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Down ||
				e.KeyCode == Keys.Up ||
				e.KeyCode == Keys.PageDown ||
				e.KeyCode == Keys.PageUp ||
				e.KeyCode == Keys.Home ||
				e.KeyCode == Keys.End)
			{
				dataGridOnKeyDown.Invoke(dataGridView, new object[] { e });
			}
		}

		public event EventHandler CodeChanged;
        public event EventHandler Execute;

		private void dataGridView_SelectionChanged(object sender, EventArgs e)
		{
			OnCodeChanged();
		}

		protected virtual void OnCodeChanged()
		{
			if (CodeChanged != null)
				CodeChanged(this, EventArgs.Empty);
		}

		protected virtual void OnExecute()
		{
			if (Execute != null)
				Execute(this, EventArgs.Empty);
		}

		private void dataGridView_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				OnExecute();
				e.Handled = true;
			}
		}

		private void dataGridView_DoubleClick(object sender, EventArgs e)
		{
			OnExecute();
		}
	}
}
